<script setup>
import TheModal from "./TheModal.vue";
import TheIcon from "./TheIcon.vue";
import { ref } from "vue";
import TheButton from "./TheButton.vue";
import { useStore } from "vuex";
const store = useStore();

//处理post上传
const imagePost = ref(null);
const description = ref("");

//处理图片预览
const imageRef = ref("");
const handleImageChange = (e) => {
  const image = e.target.files[0];
  if (!image) return;
  imageRef.value = URL.createObjectURL(image);
  imagePost.value = image;
};

const postImgDesc = () => {
  let postObj = {
    image: imagePost.value,
    description: description.value,
  };
  store.dispatch("uploadImagePost", postObj);
};
</script>

<template>
  <TheModal @close="store.commit('changeUploadVal', false)">
    <div class="postUpload">
      <label class="upload">
        <img v-if="imageRef" :src="imageRef" class="preview" />
        <TheIcon v-else icon="upload-image"></TheIcon>
        <input
          type="file"
          accept="image/*"
          class="fileChooser"
          @change="handleImageChange"
        />
      </label>
      <div class="postContent">
        <textarea
          v-model="description"
          placeholder="写点什么吧"
          class="postContentInput"
        ></textarea>
        <TheButton :reverse="true" class="pubBtn" @click="postImgDesc"
          >发布</TheButton
        >
      </div>
    </div>
  </TheModal>
</template>

<style scoped>
.postUpload {
  width: 50vw;
  height: 70vh;
  display: grid;
  grid-template-rows: 4fr 2fr;
}

.upload {
  display: grid;
  place-items: center;
  cursor: pointer;
  min-height: 0;
}

.upload > svg {
  width: 254px;
  height: 316px;
}

.fileChooser {
  opacity: 0;
  position: absolute;
}

.preview {
  width: 100%;
  height: 100%;
  object-fit: cover;
  min-height: 0;
}

.postContent {
  display: grid;
  /* grid-template-rows: 4fr 1fr; */
}

.postContentInput {
  border-bottom: none;
  resize: none;
  padding: 12px 24px;
}

.postContent .pubBtn {
  /* border: none; */
  /* width: 60px; */
  /* margin-right: 50px; */
  align-self: end;
  justify-self: end;
  position: relative;
  right: 24px;
  bottom: 18px;
}
</style>
